package tq.com.easysurfacecapture.Inner;


import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;

import java.util.ArrayList;
import java.util.List;

public class TQPathHelper {
    private TQPathHold currentPath = null;
    private List<TQPathHold> pathHolds = new ArrayList<>();

    public TQPathHelper(){}

    public void newPath(PointF pos){
        if(currentPath == null){
            currentPath = new TQPathHold(pos);
        }else{
            currentPath.startPoint(pos);
        }
    }
    public void addCurrentPoint(PointF pos){
        if(currentPath != null){
            currentPath.addPoint(pos);
        }
    }
    public void endCurrentPath(PointF pos){
        if(currentPath != null){
            currentPath.endPoint(pos);

            synchronized (pathHolds){
                pathHolds.add(currentPath);
            }

        }

        currentPath = null;
    }
    public void cancelLast(){
        synchronized (pathHolds){
            if(pathHolds.size() == 0 && currentPath == null){
                return;
            }

            pathHolds.remove(pathHolds.size() - 1);
        }
    }

    public void drawPath(Canvas canvas, Paint paint, PointF offset){
        synchronized (pathHolds){
            if(pathHolds.size() == 0 && currentPath == null){
                return;
            }

            Path all = new Path();

            for (TQPathHold hold:
                 pathHolds) {
//                Path pp = hold.getPath(offset);
//                canvas.drawPath(pp, paint);
                all.addPath(hold.getPath(offset));
            }
            if(currentPath != null){
                all.addPath(currentPath.getPath(offset));
            }

            canvas.drawPath(all, paint);
        }
    }
}
